# Contain some useful code bits that can be used later for StreamPlot
#
#
#

############################################################
# General figure customizations
#
import matplotlib as mpl
mpl.rcParams['figure.facecolor']= 'white'  # change the canvas color
mpl.rcParams['xtick.direction'] = 'out'
mpl.rcParams['xtick.major.size'] = 8       # make them slightly longer
mpl.rcParams['ytick.direction'] = 'out'
mpl.rcParams['ytick.major.size'] = 8

fig = plt.figure()    # this slows down ipython editor tremendously
ax  = fig.add_subplot(1,1,1)

# remove the ticks on the top xaxis and the right yaxis
for i, line in enumerate(ax.get_xticklines() + ax.get_yticklines()):
    if i%2 == 1:   # odd indices
        line.set_visible(False) 
plt.draw()



############################################################
# How to add a transparent histogram on the right y axis 
#
# ipython -pylab

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(100)
y = 10*np.random.randn(100)

fig = plt.figure()    # this slows down ipython editor tremendously
ax  = fig.add_subplot(1,1,1)

ax.plot(x,y)
plt.draw()

maxX      = max(x)
minY      = min(y)
yRange    = max(y) - minY
nBins     = floor(sqrt(size(y)))
binWidth  = yRange/nBins 
binHeight = array([1, 3, 4, 5, 8, 9, 8, 5, 2, 1])/1.0

colBox = (0.68, 0.85, 0.90) # light blue
for b in range(nBins):
  rect = Rectangle(xy=(maxX-binHeight[b], minY + b*binWidth), 
    width=binHeight[b], height=binWidth, alpha=1, facecolor=colBox, 
    ec="none")
  ax.add_patch(rect)

ax.set_ylim([min(y)-0.05*yRange, max(y)+0.05*yRange])
plt.draw()



############################################################
# INTERACTIVE:  Add lines when clicking with the mouse.
#
# ipython -pylab

class LineBuilder:
    def __init__(self, line):
        self.line = line
        self.xs = list(line.get_xdata())
        self.ys = list(line.get_ydata())
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self)
#
    def __call__(self, event):
        print 'click', event
        if event.inaxes!=self.line.axes: return
        self.xs.append(event.xdata)
        self.ys.append(event.ydata)
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw()
#
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0])  # empty line
linebuilder = LineBuilder(line)

